package yxy.game.pm2.active.command.signin;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_签到;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActSigninDrawRewardRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

@MessageType(proto = Command.ActSigninDrawReward, classtype = ActiveMessage.ActSigninDrawRewardRequest.class)
public class CMD_Active_签到_DrawReward extends Command<ActSigninDrawRewardRequest> {

	@Override
	public void execute(MessageSource messageSource, ActSigninDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		ActiveMessage.ActSigninDrawRewardResponse.Builder response = ActiveMessage.ActSigninDrawRewardResponse.newBuilder();
	
		int day = request.getDay();
		
		ActiveModule_签到 am = ActiveModule_签到.get(pcode,false);
		
		if(am == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
//		JSONObject json = new JSONObject();
		Currency cur = am.drawreward(pcode, day);
		if(cur == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
		
		item.setType(cur.getType().getNumber());
		item.setDataId(cur.getDataId());
		item.setNumber(cur.getNumber());
		response.addDisplay(item.build());
		response.addUpdate(item.build());
		
		//下发道具
		new CurrencyHandle().addCurrency(pcode, CCReason.活动_签到, cur);
		
		send(messageSource, response, Result.Success);
	}

	
	private void send(MessageSource messageSource, ActiveMessage.ActSigninDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
